package com.my.vertx.http;

import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.MultiMap;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.*;
import io.vertx.core.json.JsonObject;

/**
 * @author: marshal
 * @date: 2023-04-26 16:30
 */
public class NodeRedTest {

    // 创建vertx对象
    static Vertx vertx = Vertx.vertx();
    // 创建http client
    static HttpClient httpClient = vertx.createHttpClient();
    public static void main(String[] args) {
        testPost();
    }

    private static void testPost() {
        // 创建请求头
        MultiMap headers = HttpHeaders.set("content-type", "application/json");
        // 创建一个request
        Future<HttpClientRequest> future = httpClient.request(HttpMethod.DELETE, 1880, "192.168.12.37", "/flow/0ccd2033eac5efe8");
        future.onComplete((AsyncResult<HttpClientRequest> ar ) -> {
            if (ar.succeeded()) {
                HttpClientRequest request = ar.result();
                // 设置请求头
                request.headers().addAll(headers);
                // 发送request
                request
                        .send()
                        .onComplete((AsyncResult<HttpClientResponse> ar2)  -> {
                            if (ar2.succeeded()) {
                                // 拿到response
                                HttpClientResponse response = ar2.result();
                                System.out.println("Received response with status code : " + response.statusCode());
                                // 拿到response里面的body
                                response.body().onComplete(ar3 -> {
                                    if (ar3.succeeded()) {
                                        Buffer buffer = ar3.result();
                                        JsonObject jsonObject = buffer.toJsonObject();
                                        System.out.println("Received a part of the response body:");
                                        System.out.println( jsonObject.toString());
                                    }
                                });
                            }
                        });
            } else {
                System.out.println("Failure: " + ar.cause().getMessage());
            }
        });
    }
}
